package com.cs2340.taskplusplus;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * This static class holds all the methods and variables related to
 * sorting tasklists in different orders
 * @author GRILLENBANNER
 *
 */
public class Sorter 
{
	static enum FILTER {COMPLETE, LABEL, DATE, ALL};
	static enum DATEMODE {BEFORE, ON, AFTER};
	static enum DONEMODE {COMPLETE, INCOMPLETE};
	
	/**
	 * Returns a list of completed or incompleted tasks (depending on the mode that is inputted)
	 * @param list
	 * @return
	 */
	public static List<Doable> filterComplete(List<Doable> list, DONEMODE mode)
	{
		ArrayList<Doable> filteredList = new ArrayList<Doable>();
		
		for (Doable item : list){
			boolean addItem = false; //should this item be added to the list?
			
			//check if the item meets the filtering requirements
			if (mode == DONEMODE.COMPLETE && item.isDone()){
				addItem = true;
			}
			else if (mode == DONEMODE.INCOMPLETE && !item.isDone()){
				addItem = true;
			}
			
			//if so, add it to the list
			if (addItem){
				filteredList.add(item);
			}
		}
		
		return filteredList;
	}
	
	/** 
	 * Returns a list where all the items are part of one or more of the inputted labels
	 * @param list
	 * @return A filtered list.
	 */
	public static List<Doable> filterLabel(List<Doable> list, List<Label> labels)
	{
		ArrayList<Doable> filteredList = new ArrayList<Doable>();
		
		for (Doable item : list){
			boolean addItem = false; //should this item be added to the list?
			
			//check if the item meets the filtering requirements
			for (Label l : labels){
				if (item.getLabels().contains(l)){
					addItem = true;
				}
			}
			
			//if so, add it to the list
			if (addItem){
				filteredList.add(item);
			}
		}
		
		return filteredList;
	}
	
	/**
	 * Returns a list of all items BEFORE, AFTER, or ON a particular date
	 * The particular relationship is determined by the second parameter
     * @param d
	 * @return The filtered list
	 */
	public static List<Doable> filterDate(List<Doable> list, Date d, DATEMODE mode)
	{
		ArrayList<Doable> filteredList = new ArrayList<Doable>();
		
		if (d != null){
			for (Doable item : list){
				boolean addItem = false; //should this item be added to the list?
				
				//check if the item meets the filtering requirements
				if (mode == DATEMODE.BEFORE && item.isBefore(d)){
					addItem = true;
				}
				else if (mode == DATEMODE.ON && item.isOn(d)){
					addItem = true;
				}
				else if (mode == DATEMODE.AFTER && item.isAfter(d)){
					addItem = true;
				}
				
				//if so add it to the list
				if (addItem){
					filteredList.add(item);
				}
			}
		}
		
		return filteredList;
	}
}